$(function(){
  pageInx = 1
  isClick = true;
  pageFun(HOME.getQueryVariable('curId') , HOME.getQueryVariable('id') , pageInx)


  var now = 0;
  $('body').on('click' , '.answer_ul li' , function(){
    if($(this).hasClass('disabled')){return}
    var dataAnswer = $(this).attr('data_answer')
    var dataTid = $(this).attr('data_tid')
    var spanType = $('.answer_tit').attr('data_type')
    var curOkLen = $('.cur_ok').length
    var lenTxt = ''
    if(!isClick){return false}
    $(this).addClass('disabled')
    if(dataAnswer == 1){
      $(this).find('img.xx_txt').attr('src','./images/ico_right.png')
      if(spanType == 2){
        if($(this).attr('data_answer') == 1){
          now++
        }
        if(curOkLen == now){
          pageInx++
          pageFun(HOME.getQueryVariable('curId') , HOME.getQueryVariable('id') , pageInx)
          now = 0; 
        }
        return
      }
      pageInx++
      pageFun(HOME.getQueryVariable('curId') , HOME.getQueryVariable('id') , pageInx)
      
    }else{
      $(this).find('img.xx_txt').attr('src','./images/ico_wrong.png')
      $('.color-wrong  , .color-explain').show();
      for(var i=0;i<$('.answer_ul li').length -1;i++){
        if($('.answer_ul li').eq(i).attr('data_answer') == 1){
          $('.answer_ul li').eq(i).find('img.xx_txt').attr('src','./images/ico_right.png')
            if(i==0){
              lenTxt = lenTxt + 'A'
              $('.no_li_em').html(lenTxt)
            }else if(i==1){
              lenTxt = lenTxt + 'B'
              $('.no_li_em').html(lenTxt)
            }else if(i==2){
              lenTxt = lenTxt + 'C'
              $('.no_li_em').html(lenTxt)
            }else if(i==3){
              lenTxt = lenTxt + 'D'
              $('.no_li_em').html(lenTxt)
            }
          
        }
      }
    }
    isClick = false;
    
  })


  $('body').on('click' , '.prev_answer' , function(){
    pageInx--
    pageFun(HOME.getQueryVariable('curId') , HOME.getQueryVariable('id') , pageInx)
  })  
  $('body').on('click' , '.next_answer' , function(){
    if(!isClick){
      pageInx++
      pageFun(HOME.getQueryVariable('curId') , HOME.getQueryVariable('id') , pageInx)
    }else{
      layer.msg('请答题')
    }
    
  })  

 $('body').on('click' , '.agien_btn' , function(){
    pageInx = 0
    pageInx++
    pageFun(HOME.getQueryVariable('curId') , HOME.getQueryVariable('id') , 1)
    $('.answer_btn').removeClass('agien_btn').addClass('next_answer').find('img').attr('src','./images/redo.png')
    
  })
  

})






function pageFun(goodsId , categoryId , page){
  HOME.ajaxHOMEFn({
    url: HOME.baseUrl.topic,
    type: 'POST',
    dataType: 'json',
    data: {
    	goods_id : goodsId,
      category_id : categoryId,
      page:page
    }
	}, function (res) {
    if(res.data.length == 0){
      pageInx == 1 ? $('#answerBox').html(template('answerBoxTpl', {'res': res})) : layer.msg('所有题目已作答完毕,需要重新做吗？')
      $('.answer_btn').removeClass('next_answer').addClass('agien_btn').find('img').attr('src','./images/redo.png')
    }else{
      $('#answerBox').html(template('answerBoxTpl', {'res': res}));
      isClick = true;
      var windowHeight = $(window).height();
      var mainWrapHeight = $('.free_box').height();
      if(mainWrapHeight < windowHeight - 170){
          $('.free_box').height(windowHeight - 277 + 'px');
      }
    } 
  })
}